package com.csi.bumblebee.controller;

import com.csi.bumblebee.domain.user.Student;
import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.domain.util.StateCode;
import com.csi.bumblebee.service.StudentService;
import com.csi.bumblebee.util.JwtUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.csi.bumblebee.domain.util.StateCode.*;

/**
 * student表对外接口控制
 *
 * @author HUOJT
 */
@RestController
@RequestMapping("students")
@ResponseBody
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 注册学生账号
     *
     * @param student 学生对象
     * @return 结果载体，携带是否成功注册学生账号的data
     */
    @PutMapping("entry")
    public MyResult<Student> register(@RequestBody Student student) {
        MyResult<Student> myResult = new MyResult<>(null, INSERT_FAILURE, "注册失败");
        System.out.println("+++++++++++" + student.getPassword());
        if (studentService.register(student)) {
            myResult.setCode(INSERT_SUCCESS);
            myResult.setMessage("注册成功");
            System.out.println("+++++++++++" + student.getPassword());
            Student student1 = studentService.login(student);
            myResult.setData(student1);
        }
        return myResult;
    }

    /**
     * 检验token
     *
     * @param request 请求
     * @return 结果载体，携带token正确与否的boolean类型的data
     */
    @GetMapping("token")
    public MyResult<Boolean> checkUserToken(HttpServletRequest request) {
        MyResult<Boolean> myResult = new MyResult<>(FIND_FAILURE, "您的登录不安全，请重新登录");
        if (JwtUtil.parseToken(request.getHeader("token"))) {
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("登录状态正常");
            myResult.setData(true);
        }
        return myResult;
    }

    /**
     * 学生登录
     *
     * @param studentLogin 装载登录参数的学生账号
     * @return 结果载体，携带学生账号的data
     */
    @PostMapping("entry")
    public MyResult<Student> login(@RequestBody Student studentLogin) {
        MyResult<Student> myResult = new MyResult<>(FIND_FAILURE, "登录失败");
        Student student = studentService.login(studentLogin);
        if (student != null) {
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("登录成功");
            myResult.setData(student);
        }
        return myResult;
    }

    /**
     * 修改学生信息
     *
     * @param student 修改后学生信息
     * @return 结果载体，携带修改学生账号信息是否成功的data
     */
    @PostMapping("info")
    public MyResult<Boolean> updateInfo(@RequestBody Student student) {
        MyResult<Boolean> myResult = new MyResult<>(UPDATE_FAILURE, "修改信息失败");
        if (studentService.updateInfo(student)) {
            myResult.setCode(UPDATE_SUCCESS);
            myResult.setMessage("修改信息成功");
            myResult.setData(true);
        }
        return myResult;
    }

    /**
     * 修改学生账号状态
     *
     * @param student 学生对象（修改后）
     * @return 是结果载体，携带修改学生账号状态是否成功的data
     */
    @PostMapping("status")
    public MyResult<Boolean> updateStatus(@RequestBody Student student) {
        MyResult<Boolean> myResult = new MyResult<>(UPDATE_FAILURE, "修改账号状态失败");
        if (studentService.updateStatus(student)) {
            myResult.setCode(UPDATE_SUCCESS);
            myResult.setMessage("修改账号状态成功");
            myResult.setData(true);
        }
        return myResult;
    }

    /**
     * 查询学生信息，得到全部业务数据（分页）
     *
     * @param pageNum  分页页码
     * @param pageSize 每页大小
     * @param student  学生对象，承载筛选条件
     * @return 结果载体，携带student列表的data
     */
    @GetMapping("list")
    public MyResult<Student> listInfo(
            @RequestParam(name = "pageNum", required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", required = true, defaultValue = "15") Integer pageSize,
            @RequestParam(name = "student", required = false) Student student) {

        MyResult<Student> myResult = new MyResult<>(FIND_FAILURE, "查询学生数据失败");
        PageInfo<Student> studentPageInfo = studentService.listInfo(pageNum, pageSize, student);
        if (studentPageInfo.getList() != null) {
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("查询学生数据成功");
            myResult.setPageInfo(studentPageInfo);
        }
        return myResult;
    }

    /**
     * 查询指定id的学生
     *
     * @param id 学生id
     * @return 结果载体，携带学生对象的data
     */
    @GetMapping("list/{id}")
    public MyResult<Student> findById(@PathVariable(name = "id") String id) {
        MyResult<Student> myResult = new MyResult<>(FIND_FAILURE, "查询学生数据失败");
        Student student = studentService.findByStudentId(id);
        if (student != null) {
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("查询学生数据成功");
            myResult.setData(student);
        }
        return myResult;
    }

    @GetMapping("list/name/{username}")
    public MyResult<Student> findByName(@PathVariable(name = "username") String username) {
        MyResult<Student> myResult = new MyResult<>(FIND_FAILURE, "查询学生数据失败");
        Student student = studentService.findByName(username);
        if (student != null) {
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("查询学生数据成功");
            myResult.setData(student);
        }
        return myResult;
    }


    /**
     * 删除指定id的学生
     *
     * @param id 学生id
     * @return 结果载体，携带删除指定id学生是否成功的data
     */
    @DeleteMapping("list/{id}")
    public MyResult<Boolean> deleteById(@PathVariable(name = "id") String id) {
        MyResult<Boolean> myResult = new MyResult<>(DELETE_FAILURE, "彻底删除学生数据失败");
        if (studentService.deleteByStudentId(id)) {
            myResult.setCode(DELETE_SUCCESS);
            myResult.setMessage("彻底删除学生数据成功");
            myResult.setData(true);
        }
        return myResult;
    }

    @GetMapping("job/{id}")
    public MyResult<Integer> hasJobType(@PathVariable String id) {
        Integer count = studentService.hasJobType(id);
        return new MyResult<>(count, FIND_SUCCESS, "查询成功");
    }

    @PostMapping("job")
    public MyResult<Integer> countJobExam(@RequestBody Student student) {
        Integer count = studentService.countJobExam(student);
        return new MyResult<>(count, FIND_SUCCESS, "查询成功");
    }

    /**
     * 查询学生岗位偏好id
     *
     */
    @GetMapping("/jobtype/{id}")
    public MyResult<String> findByIdJobType(@PathVariable("id") String id){

        String jobtypeid = this.studentService.findByIdJobType(id);
        if (jobtypeid!=null){
            return new MyResult<>(jobtypeid, FIND_SUCCESS,"查询岗位偏好id成功");
        }

        return new MyResult<>(null, FIND_SUCCESS,"查询岗位偏好id失败");

    }

}

